/**Class represents a regular polygon where all edges have the same length.
 * 
 */
public class RegularPolygon implements Polygon, Scalable {
	
	private int edges;
    private double length;
	
	/**Creates a new regular polygon.
	 * 
	 * @param edges The number of edges.
	 * @param length The length of each edge.
	 * @throws NotEnoughEdgesException Exception if polygon is created with less than 3 edges.
	 * @throws ValueSmallerZeroException Exception if negative values are given for the length of an edge.
	 */
	public RegularPolygon(int edges, double length) throws NotEnoughEdgesException, ValueSmallerZeroException{
		if (edges >= 3) {
				this.edges = edges;
		} else {
			throw new NotEnoughEdgesException(edges);
		}
		if (length > 0) {
			this.length = length;
		} else {
			throw new ValueSmallerZeroException(length);
		}
	}

	/**Returns the number of edges of the specified polygon.
	 * 
	 * @return Interger value of the number of edges.
	 */
	@Override
	public int edges() {
		return edges;
	}

	/**Calculates the size of the area the specified polygon takes in.
	 * 
	 * @return Calculated double value of area.
	 */
	@Override
	public double area() {
		return (this.edges() * Math.pow(this.length, 2))/(4 * Math.tan(Math.PI/this.edges()));
	}

	/**Calculates the size of the outline the specified polygon takes in.
	 * 
	 * @return Calculated double value of the outline.
	 */
	@Override
	public double perimeter() {
		return this.edges() * this.length;
	}

	/**Changes the size of the specified polygon according to a given factor. 
	 * 
	 * @param factor The value by which the polygon will be scaled up or down.
	 * @throws ValueSmallerZeroException Exception if there is a negative value given as scale-factor.
	 */
	@Override
	public void scale(double factor) throws ValueSmallerZeroException{
		if (factor > 0) {
				this.length = this.length * factor;
		} else {
			throw new ValueSmallerZeroException(factor);
		}
	}	
}
